package com.androidcentralmedias;


import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
import android.util.Log;
 
public class Client {
		static int wait_t = 5;
		static int retry = 8;
		static long timeout = 3000;
        static String ip1 = new String();
        static String ip2 = new String();
        public static void enviar(String pacote, String type, int server) {
                try {
                		int i;
                		long j = 0;
                		Boolean check = false;
                		String ip=null;
                		MainActivity.msg_id++;
                		if ( type == MainActivity.type )
                			pacote = ( type + "@" + MainActivity.msg_id + "@" + MainActivity.ip + "@" + pacote);
                        // Retrieve the ServerName
                		if (server==1) ip =ip1;
                		if (server==2)	ip=ip2;
                		InetAddress serverAddr = InetAddress.getByName(ip);//ip 10.0.2.2 para emulador
                		/* Create new UDP-Socket */
                        DatagramSocket socket = new DatagramSocket();
                        /* Prepare some data to be sent. */
                        byte[] outData = (pacote).getBytes();
                       
                        /* Create UDP-packet with
                         * data & destination(url+port) */
                        DatagramPacket packet = new DatagramPacket(outData, outData.length,     serverAddr, 1235);
                        if (server==2) packet = new DatagramPacket(outData, outData.length,     serverAddr, 1232); //Client do Android e Server do PC na 1235
                        Log.d("UDP", "C: Sending: " + new String(packet.getData(),0, packet.getLength()));
                        /* Send out the packet */
                        if ( type == MainActivity.type ) {  
	                    	while(!check) {
	                        	j++;
	                        	socket.send(packet);
	                        	System.out.println("pacote: " + pacote);
		             			//se message == pacote entao o android recebeu o pacote e mandou ele de volta pode continuar
		             			for ( i=0; i < retry && !check; i++) {
	             					if (MainActivity.ok_mensagem!=null && MainActivity.ok_mensagem.equals(pacote)) {
	             						check=true;
	             						Log.e("UDP", "C: Sucess: Message received!");
	             					}
		             				else {
	             						Thread.sleep(wait_t);
	             					}
		             			}
		             			if ( j > (timeout/(wait_t*retry)) ) {
		             				check = true;
		             				Log.e("UDP", "C: Error: did not receive answer.");
		             			}
	                        }
                        }
                        else {
                        	socket.send(packet);
                        }
                } catch (Exception e) {
                        Log.e("UDP", "C: Error", e);
                }
        }
}